﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Application;
using RBAC_Application.Dto.Input;
using RBAC_Application.Dto.Output;
using RBAC_Application.Repository.Interface;
using RBAC_Application.Service.RBAC;
using Standard;

namespace B.MES.Login.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
     
        
        private readonly IRbacService _rbacService;

        public LoginController(IRbacService rbacService)
        {
            _rbacService = rbacService;
        }



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public ApiResult<LoginOutput> Login([FromBody] LoginInput input)
        {
            try
            {
                var result = _rbacService.ValidateUserLogin(input.UserName, input.Password);

                if (!result.Success)
                {
                    return new ApiResult<LoginOutput>
                    {
                        Code = ApiCode.ParameterError,
                        Message = result.Message
                    };
                }
               
                return new ApiResult<LoginOutput>
                {
                    Code = ApiCode.Success,
                    ResultData = result,
                    Message = result.Message,// 这里直接返回“登录成功”
          
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<LoginOutput>
                {
                    Code = ApiCode.InternalError,
                    Message = ex.Message
                };
            }
        }
    }
}
